<template>
    <view class="income">
        <view class="order_date">
            <view class="date_item" style="margin-right: 32rpx">
                <p>Start Date</p>
                <view class="item_choose" @click.stop="startShow = true">
                    <text>{{
                        startDate ? formatDate(startDate) : "Select date"
                    }}</text>
                    <image src="/static/business/time.png" mode=""></image>
                </view>
            </view>
            <view class="date_item">
                <p>End Date</p>
                <view class="item_choose" @click.stop="endShow = true">
                    <text>{{
                        endDate ? formatDate(endDate) : "Select date"
                    }}</text>
                    <image src="/static/business/time.png" mode=""></image>
                </view>
            </view>
        </view>
        <ul>
            <!-- @click.stop="handleDetails(item)" -->
            <p style="margin-top: 24rpx">Income Details</p>
            <li v-for="(item, index) in incomeList" :key="index">
                <view class="income_left">
                    <text>${{ item.money || "0.00" }}</text>
                    <text>{{ item.content }}</text>
                </view>
                <view class="income_right">
                    {{ item.ctime }}
                </view>
            </li>
        </ul>
        <view class="empty" v-if="incomeList.length <= 0">
            <image src="@/static/list.png" mode=""></image>
            <p>No income for now</p>
        </view>
        <u-loadmore
            v-if="incomeList.length > 0 && hasMore"
            :status="loadStatus"
        />
        <u-datetime-picker
            :show="startShow"
            v-model="startDate"
            mode="date"
            @confirm="handleStart"
        ></u-datetime-picker>
        <u-datetime-picker
            :show="endShow"
            v-model="endDate"
            mode="date"
            @confirm="handleEnd"
        ></u-datetime-picker>
    </view>
</template>

<script>
import { getIncomeList } from "@/lib/business.js";
import dayjs from "dayjs";
import { showFailure } from "@/lib/helper";
export default {
    data() {
        return {
            startShow: false,
            endShow: false,
            startDate: "",
            endDate: "",
            incomeList: [],
            loadStatus: "loadmore",
            currentPage: 1,
            pageSize: 10,
            hasMore: false,
        };
    },
    onLoad() {
        this.loadData();
    },
    onReachBottom() {
        if (this.hasMore) {
            this.currentPage = this.currentPage + 1;
            this.loadData();
        }
    },
    methods: {
        formatDate(timestamp) {
            return dayjs(timestamp).format("YYYY-MM-DD");
        },
        loadData(isReset) {
            this.loadStatus = "loading";
            let obj = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
            };
            if (this.startDate) {
                obj["sdate"] = this.startDate;
            }
            if (this.endDate) {
                obj["edate"] = this.endDate;
            }
            getIncomeList(obj)
                .then((res) => {
                    uni.hideLoading();
                    this.loadStatus = "no-more";
                    this.hasMore = res.data.hasMore;
                    if (isReset) {
                        this.incomeList = res.data.records;
                    } else {
                        this.incomeList = this.incomeList.concat(
                            res.data.records
                        );
                    }
                })
                .catch((res) => {
                    this.loadStatus = "no-more";
                    uni.hideLoading();
                    showFailure();
                });
        },
        handleStart(e) {
            this.startDate = dayjs(e.result).format("YYYY-MM-DD");
            this.loadData(true);
            this.startShow = false;
        },
        handleEnd(e) {
            this.endDate = dayjs(e.result).format("YYYY-MM-DD");
            this.loadData(true);
            this.endShow = false;
        },
        handleDetails(item) {
            uni.navigateTo({
                url: "./order_details?orderid=" + item.orderid,
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.empty {
    text-align: center;

    image {
        width: 376rpx;
        height: 376rpx;
    }

    p {
        font-size: 28rpx;
        color: #999999;
    }
}
.income {
    padding: 32rpx 28rpx;

    .order_date {
        display: flex;

        .date_item {
            p {
                font-weight: 500;
                font-size: 28rpx;
                color: #666666;
            }

            .item_choose {
                width: 331rpx;
                height: 100rpx;
                background: #f3f4f6;
                border-radius: 16rpx;
                border: 2rpx solid #ccd1d7;
                margin-top: 16rpx;
                display: flex;
                justify-content: space-between;
                align-items: center;

                image {
                    width: 40rpx;
                    height: 40rpx;
                    margin-right: 20rpx;
                }

                text {
                    font-weight: 500;
                    font-size: 28rpx;
                    color: #b5b5b5;
                    margin-left: 26rpx;
                }
            }
        }
    }

    ul {
        > p {
            font-family: Manrope, Manrope;
            font-weight: bold;
            font-size: 32rpx;
            color: #1c120d;
        }

        li {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 20rpx;
            background: #ffffff;
            box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 0, 0, 0.08);
            border-radius: 16rpx;
            padding: 24rpx 32rpx;

            .income_left {
                text {
                    display: block;
                    font-weight: 800;
                    font-size: 36rpx;
                    color: #fc721a;
                }

                text:nth-child(2) {
                    font-weight: 500;
                    font-size: 28rpx;
                    color: #666666;
                    margin-top: 10rpx;
                }
            }

            .income_right {
                font-size: 24rpx;
                color: #888888;
                width: 154rpx;
                text-align: right;
            }
        }
    }
}
</style>
